package org.bambrikii.examples.rutext;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class SentenceBuilder {
	private List<String> words = new LinkedList<String>();
	private static String NEW_LINE = System.getProperty("line.separator");

	private static List<String> WORD_SPLITTERS = new ArrayList<String>() {
		private static final long serialVersionUID = 1L;
		{
			add(null);
			add("");
			add(NEW_LINE);
			add("\r");
			add("\n");
		}
	};

	public SentenceBuilder addWord(String word) {
		words.add(word);
		return this;
	}

	public String build() {
		StringBuilder sb = new StringBuilder();
		String prev = null;
		for (String word : words) {
			if (!WORD_SPLITTERS.contains(prev)) {
				sb.append(" ");
			}
			sb.append(word);
			prev = word;
		}
		return sb.toString();
	}

	public void addNewLine() {
		words.add(NEW_LINE);
	}
}
